<?php
//app登录
namespace App\Http\Controllers\App;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;
use App\Models\ShopClerk;
use App\Models\UserCheck;
class AuthController extends Controller
{

    //app登录
    public function login(Request $request){
//         $account = $request->mobile;     //手机号
//         $password = $request->password;  //密码
//         $device_id = $request->device_id;//手机标识id
//         $code = $request->code;          //手机验证码
//         $role = $request->role;          //role=0商家 role=1店员

         if($request->code){//验证码登录
             //验证手机号
             if(!preg_match("/^1[345678]{1}\d{9}$/",$request->mobile)){
                 return show(201,'手机号码格式不正确');
             }
             //验证手机ID
             if(!$request->device_id){
                 return show(201,'缺少手机标识');
             }
             //验证验证码
             $code=Cache::get('app_'.$request->mobile);
             if(!$code){
                 return show(201,'请先发送验证码');
             }else{
                 if($request->code != $code['code']){
                       return show(201,'验证码不正确');
                 }
             }
             //判断登录角色
             if($request->role=='0'){//商家
                 $UserCheck = UserCheck::where('mobile',$request->mobile)
                                        ->select(['id','mobile','name','sex','money','point','role','avatar','password'])
                                        ->first();
                 //判断会员是否存在
                 if(!$UserCheck){
                    return show(301,'商家不存在');
                 }
                 //判断会员的角色
                 if($UserCheck->role != '1'){
                     return show(301,'你还不是商家');
                 }else{
                     $shopclerk = $UserCheck;
                     $shopclerk->role = -1;//商家标识
                 }

             }else if($request->role=='1'){//店员
                 $shopclerk=ShopClerk::where('mobile',$request->mobile)->first();
                 if(!$shopclerk){
                     return show(301,'店员不存在');
                 }
             }else{//错误
                 return show(201,'请选择角色');
             }
             $shopclerk->device_id=$request->device_id;
             unset($shopclerk->password);
             $rand = sha1($request->device_id.$request->mobile.time());
             Cache::put($rand,$shopclerk,60*24*7);
             return show(200,'登陆成功',['token'=>$rand,'shopclerk'=>$shopclerk]);
         }else{

             if(!preg_match("/^1[345678]{1}\d{9}$/",$request->mobile)){
                 return show(201,'手机号码格式不正确');
             }
             if(!$request->device_id){
                 return show(202,'缺少手机标识');
             }
             if(!$request->password){
                 return show(203,'请输入密码');
             }

             //判断登录角色
             if($request->role=='0'){//商家
                 $UserCheck = UserCheck::where('mobile',$request->mobile)
                                       ->select(['id','mobile','name','sex','role','money','point','avatar','password'])
                                       ->first();
                 //判断会员是否存在
                 if(!$UserCheck){
                     return show(301,'商家不存在');
                 }
                 //判断会员的角色
                 if($UserCheck->role != 1){
                     return show(301,'你还不是商家');
                 }else{
                     $shopclerk = $UserCheck;
                     $shopclerk->role = -1;//商家标识
                 }

             }else if($request->role=='1'){//店员
                 $shopclerk=ShopClerk::where('mobile',$request->mobile)->first();
                 if(!$shopclerk){
                     return show(301,'店员不存在');
                 }
             }else{//错误
                 return show(201,'请选择角色');
             }
             //验证密码
             if(!Hash::check($request->password,$shopclerk->password))
             {
                 return show(204,"登录密码不正确");
             }
             //随机字符串
             $shopclerk->device_id=$request->device_id;
             unset($shopclerk->password);
             $rand = sha1($request->device_id.$request->mobile.time());
             Cache::put($rand,$shopclerk,60*24*7);
             return show(200,'登陆成功',['token'=>$rand,'shopclerk'=>$shopclerk]);
         }
    }

    //app登出
    public function logout(Request $request) {
        $value = Cache::pull($request->token);
        if($value){
            return show(200,'登出成功');
        }else{
            return show(201,'无效token');
        }
    }
    //手机验证码登录
    public function sendSms(Request $request){
        if(preg_match("/^1[345678]{1}\d{9}$/",$request->mobile)){
            //生成验证码存入缓存
            $code = rand(100000,999999);
            Cache::put('app_'.$request->mobile, ['mobile'=>$request->mobile,'code'=>$code], 5);
            //发送短信
            $url="http://send.18sms.com/msg/HttpBatchSendSM?account=vd736g&pswd=dTP8X306&mobile=".$request->mobile."&msg=%E6%82%A8%E7%9A%84%E9%AA%8C%E8%AF%81%E7%A0%81%E6%98%AF%EF%BC%9A".$code."&needstatus=true&extno=";
            file_get_contents($url);
            return ["code"=>200,"info"=>"发送成功","data"=>[]];
        }else{
            return ["code"=>201,"info"=>"手机号错误","data"=>[]];
        }
    }
    //更新token
    public function refreshToken(Request $request){
        $data = Cache::get($request->token);
        if(!$data){
            return show(201,'token已过期，请重新登录');
        }else{
            if($request->device_id != $data->device_id){
                return show(202,'手机标识错误，不能刷新');
            }
            //清除旧token
            Cache::forget($request->token);
            $rand = sha1($request->device_id.$data->mobile.time());
            Cache::put($rand,$data,60*24*7);
            return show(200,'刷新登陆成功',['token'=>$rand,'shopclerk'=>$data]);
        }
    }
}
